﻿using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xarmarin.GoldFinger.Engine.Core;
using Xarmarin.GoldFinger.Models;
using Xarmarin.GoldFinger.Models.AutoModels;

namespace Xarmarin.GoldFinger.Data
{
    public class DbContext
    {
        private SQLiteConnection db = new SQLiteConnection(CoreEngine.ConnectionString);
        /// <summary>
        /// 初始化数据库
        /// </summary>
        public void CreatedDbTables()
        {
            db.CreateTable<Auto_Subject>();

            db.CreateTable<Auto_Sys_OperationRecord>();
            
        }
        /// <summary>
        /// 清除数据库
        /// </summary>
        public void CleanDtTables()
        {
            db.DropTable<Auto_Subject>();
            db.DropTable<Auto_Sys_OperationRecord>();
        }
        /// <summary>
        /// 判断表是否存在，不存在则创建表
        /// </summary>
        /// <returns></returns>
        public bool ExistsTable<T>(string tablename)
        {
            List<Auto_Subject> pret = db.Query<Auto_Subject>("select name from sqlite_master where type='table' and name='"+tablename+ "';");
            if (pret.Any())
            {
                return true;
            }
            else
            {
                db.CreateTable<T>();
            }
            return false;
        }
    }
}
